
const db = require('../db/index')


// 导入生成token的包
const jwt = require('jsonwebtoken')
//导入全局配置的token的包
const config = require('../config')

// 用户注册函数
exports.regist_user = function (req, res) {
    const userinfo = req.body
    console.log(userinfo);
    db.query('select * from user where username=?', userinfo.username, function (err, results) {
        if (err) return res.send({ status: 1, message: err.message })
        if (results.length > 0) return res.send({ status: 1, message: '这个绅士名已经有狼人用了' })

        db.query('insert into user set ?', { username: userinfo.username, password: userinfo.password }, function (err, results) {
            // sql语句错误
            if (err) return res.send({ status: 1, message: err.message })
            // 查询失败
            if (results.affectedRows !== 1) return res.send({ status: 1, message: '失败，不是哔咔的错' })
            res.send({ status: 0, message: '注册成功' })
        })

    })
}

// 用户学生登录的函数
exports.login_user = function (req, res) {
    const userinfo = req.body
    db.query('select * from user where username=?', userinfo.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length !== 1) return res.cc('该用户没有注册')
        // bycript是加密密码的，用bycript.compareSync(,)的方法去对比密码是否正确
        if (userinfo.password !== results[0].password) return res.cc('密码不对，重新输入')
        // 生成一个token值并返回
        const user = { ...results[0], password: '' }
        const token = jwt.sign(user, config.jwtSecretKey, { expiresIn: '10h' })
        res.send({ status: 0, message: '欢迎进来', token: 'Bearer ' + token })
    })
}

// 教师登录的函数
exports.login_teacher = function (req, res) {
    db.query('select * from teacher_login where username=?', req.body.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('该用户没有注册哦')
        // console.log(results);

        if (req.body.password !== results[0].password)
            return res.cc('密码错误')
        // 剔除重要的信息
        // console.log({ ...results[0], password: '' });
        const teacher = { ...results[0], password: '' }
        const token = jwt.sign(teacher, config.jwtSecretKey, { expiresIn: '5h' })
        res.send({
            status: 0,
            message: '登录成功',
            token: 'Bearer ' + token
        })
    })
}

//系管理登录函数
exports.login_minAdmin = function (req, res) {
    const userinfo = req.body
    db.query('select * from minAdmin where username=?', userinfo.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有注册')
        if (userinfo.password !== results[0].password) return res.cc('密码错误')
        const user = { ...results[0], password: '' }
        const token = jwt.sign(user, config.jwtSecretKey, { expiresIn: '5h' })
        res.send({
            status: 0,
            message: '登录成功',
            token: 'Bearer ' + token
        })
    })
}

//学院管理登录函数
exports.login_bigAdmin = function (req, res) {
    db.query('select * from bigadmin where username=?', req.body.username, function (err, results) {
        if (err) return res.cc(err)
        if (results.length < 1) return res.cc('没有注册')
        if (req.body.password !== results[0].password)
            return res.cc('密码错误')
        const user = { ...results[0], password: '' }
        const token = jwt.sign(user, config.jwtSecretKey, { expiresIn: '5h' })
        res.send({
            status: 0,
            message: '欢迎',
            token: 'Bearer ' + token
        })
    })
}





